package com.levy.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKProxy {

	public static void main(String[] args) {

		// 需要代理的接口，被代理类实现的多个接口都必须在这里定义
		Class[] proxyInterface = new Class[] { IBusiness.class, IBusiness2.class };

		// 构建AOP的Advice，这里需要歘如业务的实例
		LogInvocationHandler handler = new LogInvocationHandler(new Business());

		// 生成代理类的字节码加载器
		ClassLoader classLoader = JDKProxy.class.getClassLoader();

		// 织入器，织入代码并生成代理类
		IBusiness2 proxyBusiness = (IBusiness2) Proxy.newProxyInstance(classLoader, proxyInterface, handler);

		// 使用代理类的实例调用方法
		proxyBusiness.doSomeThing2();
		((IBusiness) proxyBusiness).doSomeThing();

	}
}

/**
 * JDK 实现的动态代理 打印日志切面类
 */
class LogInvocationHandler implements InvocationHandler {

	private Object target;// 目标对象

	LogInvocationHandler(Object target) {
		this.target = target;
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// 执行原有的逻辑
		Object rev = method.invoke(target, args);

		// 执行织入的日志，你可以控制那些方法执行切入逻辑
		if (method.getName().equals("doSomeThing2")) {
			System.out.println("记录日志");
		}

		return rev;
	}

}

interface IBusiness {
	public boolean doSomeThing();
}

interface IBusiness2 {
	public boolean doSomeThing2();
}

class Business implements IBusiness, IBusiness2 {

	public boolean doSomeThing() {
		System.out.println("执行业务逻辑");
		return false;
	}

	public boolean doSomeThing2() {
		System.out.println("执行业务逻辑2");
		return false;
	}

}